<?php

namespace App\Admin\Controllers;

use App\Models\FhResource;
use App\Models\FhResourseList;
use App\Models\FhCategory;
use App\Models\FhProduct;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class FhResourceController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     * 
     * 
     */

    protected function grid()
    {
        return Grid::make(new FhResource(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('img')->image('',100,200);
            $grid->column('pid','归属类别')->display(function ($pid) {
                return FhCategory::where('id', $pid)->value('c_name');
            });
            $grid->column('product_id','关联产品')->display(function ($product_id) {
                return FhProduct::where('id', $product_id)->value('name');
            });;
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new FhResource(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('img');
            $show->field('r_type');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {

        return Form::make(FhResource::with(["list"]), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->image('img')->required()->move('images/'.date("Ymd",time()));
            $form->select('pid')->options('/apis/top')->required();
            $form->select('product_id',"关联产品")->options('/apis/getproduct')->required();
            $form->switch('show')->default(1);
            $form->number('sort')->min(1)->default(1);

            $form->hasMany('list', __('下载资源'), function (Form\NestedForm $form) {
                // $form->display('id');
                $form->text('r_name',"文件名称")->required();
                $form->textarea('r_desc',"文件备注")->required();
                $form->radio('r_type',"文件类型")->options([1=>"开发包",2=>"说明书",3=>"规格书"])->default(1);
                $form->radio('r_addr',"存储地址")->options([1=>"百度云",2=>"本地上传"])->default(1);               
                $form->text('r_url','上传文件');                           
                $form->file('n_url','上传文件');                     
                
                $form->switch('show','是否显示')->default(1);
            })->required();

            $form->saving(function(Form $form){
                $formId = $form->model()->id;
                if($formId){
                    $product_id = $form->input("product_id");
                    if($form->input('list')){
                        $datas = [];
                        foreach ($form->input('list') as $vals)
                        {
                            $val['id'] = $vals['id'];
                            $val['p_id'] = $formId;
                            $val['r_desc'] = $vals['r_desc'];
                            $val['r_name'] = $vals['r_name'];
                            $val['r_addr'] = $vals['r_addr'];
                            $val['show'] = $vals['show'];
                            $val['r_url'] = $vals['r_url'];
                            $val["n_url"]= $vals["n_url"];
                            $val["product_id"]= $product_id;
                            $datas[] = $val;
                        }
                        $form->list = $datas;
                    }
                    return;
                }
            });

            $form->saved(function (Form $form, $result) {
                // 判断是否是新增操作
                $product_id = $form->input("product_id");
                
                if ($form->isCreating()) {
                    // 自增ID
                    // 也可以这样获取自增ID
                    $newId = $result;
                    if (! $newId ) {
                        return $form->error('数据保存失败');
                    }
                  
                    if($form->input('list')){
                        $datas = [];
                        foreach ($form->input('list') as $vals)
                        {
                           
                            $val['p_id'] = $newId ;
                            $val['r_name'] = $vals['r_name'];
                            $val['r_addr'] = $vals['r_addr'];
                            $val['r_desc'] = $vals['r_desc'];
                            $val['show'] = $vals['show'];
                            $val['r_url'] = $vals['r_url'];
                            $val["n_url"]= $vals["n_url"];
                            $val["product_id"]= $product_id;
                            $datas[] = $val;
                        }
                      $form->list = $datas;
                    }
                    return;
                }
                
                // 修改操作
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
    public function type(){
       
        return response()->json();
    }
}
